/*
 *  Copyright 2012 Dirk Vranckaert
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package eu.vranckaert.vvl.activities.preferences;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import eu.vranckaert.vvl.R;
import eu.vranckaert.vvl.activities.preferences.alarm.AlarmsActivity;
import eu.vranckaert.vvl.utils.context.Constants;

/**
 * User: DIRK VRANCKAERT
 * Date: 28/02/12
 * Time: 8:16
 */
public class PreferenceActivity extends android.preference.PreferenceActivity {
    private static final String LOG_TAG = PreferenceActivity.class.getSimpleName();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_preferences);

        configurePreferences(PreferenceActivity.this);
        createPreferences(PreferenceActivity.this);
    }

    private void configurePreferences(android.preference.PreferenceActivity ctx) {
        ctx.getPreferenceManager().setSharedPreferencesName(Constants.Preferences.PREFERENCES_NAME);
    }

    private void createPreferences(android.preference.PreferenceActivity ctx) {
        PreferenceScreen preferences = ctx.getPreferenceManager().createPreferenceScreen(ctx);
        setPreferenceScreen(preferences);

        //Category TRAFFIC INFO
        createCategoryButton(ctx, preferences, R.string.preferences_category_traffic_info_title, TrafficInfoPreferenceActivity.class);

        //Category ALARMS
        createCategoryButton(ctx, preferences, R.string.preferences_category_alarms_title, AlarmsActivity.class);
    }

    private void createCategoryButton(final Context ctx, final PreferenceScreen preferences,
                                      final int textResId, final Class preferenceActivity) {
        Preference preferencesItem = new Preference(ctx);
        preferencesItem.setTitle(textResId);
        preferences.addPreference(preferencesItem);
        preferencesItem.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Intent intent = new Intent(PreferenceActivity.this, preferenceActivity);
                startActivity(intent);
                return true;
            }
        });
    }
}